package com.um.jdy.common.system.service.impl;

import com.um.jdy.common.system.dao.ParamDao;
import com.um.jdy.common.system.entity.po.Param;
import com.um.jdy.common.system.entity.vo.ParamTreeNode;
import com.um.jdy.common.system.entity.vo.ParamVO;
import com.um.jdy.common.system.service.ParamService;
import com.um.springboot.starter.orm.entity.Response;
import com.um.springboot.starter.orm.service.TreeServiceImpl;
import com.um.springboot.starter.utils.UMBeanUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 服务实现类：系统参数表
 */
@Service
@Transactional
@Slf4j
public class ParamServiceImpl extends TreeServiceImpl<ParamDao, Param, ParamTreeNode> implements ParamService {

    /**
     * 查询所有字典
     * @returnVO
     */
    @Override
    public Response getAllParam() {
        Map<String, List<ParamVO>> map = new LinkedHashMap<String,List<ParamVO>>();
        List<Param> allParam = dao.getList ("is_leaf","1","sort_no","asc");
        String path = null;
        List<ParamVO> list = null;
        for(Param param : allParam){
            ParamVO paramVO = UMBeanUtils.copyBean(param,ParamVO.class);
            if(path == null || !path.equals(param.getPath())){
                path = param.getPath()+"";
                if(map.containsKey(path))
                    list = map.get(path);
                else{
                    list = new ArrayList<>();
                    map.put(path, list);
                }
                list.add(paramVO);

            }else{
                list.add(paramVO);
            }
        }

        return Response.success(map);
    }

    /**
     * 查询所有字典
     * @returnVO
     */
    @Override
    public Response getAllParamForV2() {
        Map<String,List<ParamVO>> map = new LinkedHashMap<String,List<ParamVO>>();
        List<Param> allParam = dao.getList ("is_leaf","Yes","sort_no","asc");
        String path = null;
        List<ParamVO> list = null;
        for(Param param : allParam){
            ParamVO paramVO = UMBeanUtils.copyBean(param,ParamVO.class);
            if(path == null || !path.equals(param.getPath())){
                path = param.getPath()+"";
                if(map.containsKey(path))
                    list = map.get(path);
                else{
                    list = new ArrayList<>();
                    map.put(path, list);
                }
                list.add(paramVO);

            }else{
                list.add(paramVO);
            }
        }

        return Response.success(map);
    }

}
